
// 防抖
export class Debounce{
  private timeout:number
  private task:() => void
  private setTimeoutId:any = 0
  constructor(task:() => void, timeDelay:number){
    this.setTimeoutId
    this.task = task
    this.timeout = timeDelay
  }
  public excute(){
    clearTimeout(this.setTimeoutId)
    this.setTimeoutId = setTimeout(() => {
      this.task()
    }, this.timeout);
  }
}

// 节流
export class Throttle{
  private timeout:number
  private task:() => void
  private setTimeoutId:any = 0
  private ok = true //是否执行
  constructor(task:() => void, timeDelay:number){
    this.setTimeoutId
    this.task = task
    this.timeout = timeDelay
  }
  public excute(){
    if(this.ok){
      this.task()
      this.ok = false
      setTimeout(() => {
        this.ok = true
      }, this.timeout);
    }
  }
}
// 防抖
export function toDebounce(fn:Function, timeout:number){
  let id = 0
  return function(...args:any[]){
    clearTimeout(id)
    id = setTimeout(() => {
      fn(...args)
    }, timeout) as any as number
  }
}
// 节流
export function toThrottle(fn:Function, timeout:number, ...args:any[]){
  let id = 0
  let excute = true // 是否可以执行函数
  return function(...args:any[]){
    if(excute){
      fn(...args)
      excute = false
      setTimeout(() => {
        excute = true
      }, timeout);
    }
  }
}